<?php

//include ('common_validation.php');

function scheme_form_validation($form) {


  // Bad character validations for title field

  chkbadchar($form['title']['#value'], $form['title']['#title'], 'title');
  chkbadchar($form['field_scheme_description']['und'][0]['value']['#value'],$form['field_scheme_description']['und'][0]['value']['#title'],'field_scheme_description');
  chkbadchar($form['field_scheme_how_to_avail']['und'][0]['value']['#value'], $form['field_scheme_how_to_avail']['und'][0]['value']['#title'], 'field_scheme_how_to_avail');
  chkbadchar($form['field_scheme_keywords']['und'][0]['value']['#value'], $form['field_scheme_keywords']['und'][0]['value']['#title'], 'field_scheme_keywords');
  chkbadchar($form['field_scheme_funding_pattern']['und'][0]['value']['#value'], $form['field_scheme_funding_pattern']['und'][0]['value']['#title'], 'field_scheme_funding_pattern');
  chkbadchar($form['field_scheme_eligibility']['und'][0]['value']['#value'], $form['field_scheme_eligibility']['und'][0]['value']['#title'], 'field_scheme_eligibility');

	// minimum length charcter
  	minilength_validation($form['title']['#value'], 5, $form['title']['#title'], 'title');
  	minilength_validation($form['field_scheme_keywords']['und'][0]['value']['#value'], 5, $form['field_scheme_keywords']['und'][0]['value']['#title'],'field_scheme_keywords');
  	minilength_validation($form['field_scheme_description']['und'][0]['value']['#value'], 5, $form['field_scheme_description']['und'][0]['value']['#title'], 'field_scheme_description');
	minilength_validation($form['field_scheme_how_to_avail']['und'][0]['value']['#value'], 5, $form['field_scheme_how_to_avail']['und'][0]['value']['#title'], 'field_scheme_how_to_avail');
	minilength_validation($form['field_scheme_funding_pattern']['und'][0]['value']['#value'], 5, $form['field_scheme_funding_pattern']['und'][0]['value']['#title'], 'field_scheme_funding_pattern');
	minilength_validation($form['field_scheme_eligibility']['und'][0]['value']['#value'], 5, $form['field_scheme_eligibility']['und'][0]['value']['#title'], 'field_scheme_eligibility');
  /*
   * Reference url validation
   */

  $reference_url = $form['field_scheme_reference_url']['und'][0]['value']['#value'];
  $title = $form['field_scheme_reference_url']['und'][0]['value']['#title'];
  $required = 1;
  //check_reference_url($reference_url, $title, 'field_scheme_reference_url', $required);

  /*
  ** Scheme date validation
  */

  $introduced_on = $form['field_scheme_introduced_on']['und'][0]['value']['date']['#value'];
  $valid_upto = $form['scheduler_settings']['unpublish_on']['date']['#value'];//$form['field_scheme_valid_upto'][0]['value']['date']['#value'];


  /*
   * Beneficiaries field validaiton
   */
  $beneficiaries = strtolower(trim($form['field_scheme_beneficiaries']['value']['#value']));
  if ($beneficiaries == 'please select a beneficiaries') {
    form_set_error('field_scheme_beneficiaries', t('%title field is required',
                                                      array('%title' => $form['field_scheme_beneficiaries']['#title'])));
  }


  /*
   * Benefit types field validaiton
   */

  $benefit_types = strtolower(trim($form['field_scheme_benfits_type']['value']['#value']));

  if ($benefit_types == 'please select a benfits') {
    form_set_error('field_scheme_benfits_type', t('%title field is required',
                                                       array('%title' => $form['field_scheme_benfits_type']['#title'])));
  }


	/*
	*Beneficiaries type

	*/

	$beneficiaries =  trim($form['field_scheme_beneficiaries']['und']['#value']);
	$age_from = trim($form['field_scheme_eligible_age_from']['und']['#value']);
	$to_age = $form['field_scheme_eligible_age_to']['und']['#value'];

	if($beneficiaries == '3' && $age_from < 60 && $age_from !='')
	{
		 form_set_error('field_scheme_eligible_age_from', t('%age Field Value Is Must Greater Than Age 60.Because Beneficiaries type Is Senior Citizen',
                                                       array('%age' => $form['field_scheme_eligible_age_from']['#title']) ));


	}

	if ( $beneficiaries == '3' && $age_from >= $to_age && $to_age != '') {
			 form_set_error('field_scheme_eligible_age_to', t('Age From Field Value is must Smaller than %to field Value',
															array('%to' => $form['field_scheme_eligible_age_to']['#title'])));
		   }

	if ($age_from >= $to_age && $beneficiaries != '3' && $to_age != '') {
			 form_set_error('field_scheme_eligible_age_to', t('Age From Field Value is must Smaller than %to field Value',
															array('%to' => $form['field_scheme_eligible_age_to']['#title'])));
		   }
	if($to_age != '' && $age_from == ''){
			 form_set_error('field_scheme_eligible_age_from', t('Age From Field Value is empty',
															array('%to' => $form['field_scheme_eligible_age_to']['#title'])));
		   }
  	if ($beneficiaries != '3' && $age_from != '' && $to_age == '' ) {
			 form_set_error('field_scheme_eligible_age_to', t('%to field Value is Required',
															array('%to' => $form['field_scheme_eligible_age_to']['#title'])));
		   }

  /*
   * Eligibility Income field validaiton
   */

   $income = $form['field_scheme_eligible_income']['und'][0]['value']['#value'];
   check_integer($income, $form['field_scheme_eligible_income']['und'][0]['value']['#title'], 'field_scheme_eligible_income');

   // Date validation

   check_scheduler_date_expire($form['scheduler_settings']['unpublish_on']['date']['#value'],'Vaild Upto');

   check_date_expire($form['field_scheme_introduced_on']['und'][0]['value']['date']['#value'],$form['field_scheme_introduced_on']['und'][0]['value']['date']['#title']);

	// Form Type validtion

  $form_type = $form['field_scheme_type'];

  if ($form_type['und']['#value'] == 'online') {
    url_validation($form['field_scheme_reference_url']['und'][0]['value']['#value']);
  }

  if ($form_type['und']['#value'] == 'download') {

    $filename = $form['field_scheme_upload']['und'][0]['#file'];
	$filename->filename;
    file_upload_validation_D7($filename->filename,$form['field_scheme_type']['und']['#title']);
  }


} //function scheme_form_validation()

function scheme_load_validation() {
  $stat = new set_static_variable();
  $stat->set_edit('yes');
}

function scheme_presave_validation($node) {
  $reference_url = $node->field_scheme_reference_url[0]['value'];
  $node->field_scheme_reference_url[0]['value'] = url_validation($reference_url);
} //function scheme_presave_validation()


